<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\validate\MenuValidate;
use think\App;
use think\facade\View;

class Menus extends Common
{
    protected $modelClass = \app\common\model\Menus::class;
    protected $validator   = MenuValidate::class;

    public function __construct(App $app)
    {
        parent::__construct($app);

        // 筛选的菜单列表
        $list = $this->model ? $this->model->getSelectLists([], ['menu_sort' => 'ASC']) : [];
        View::assign('menus', $list);
    }

    protected function setSearchWhereFilter(array &$params = []):void
    {
        parent::setSearchWhereFilter($params); // TODO: Change the autogenerated stub

        if (!isset($params['parent_id'])) $params['parent_id'] = 0;
        if ( isset($params['parent_id']) && $params['parent_id'] > -1 ) {
            $params['where'][] = ['parent_id', '=', $params['parent_id']];
        }

        if ( isset($params['is_show']) && $params['is_show'] > -1 ) {
            $params['where'][] = ['is_show', '=', $params['is_show']];
        }

        // 默认排序
        $params['order'] = ['menu_sort', 'ASC'];
    }

    public function detail()
    {
        $home_view_path = APP_PATH . config('app.default_app') . DIRECTORY_SEPARATOR . config('view.view_dir_name') . DIRECTORY_SEPARATOR . lcfirst(config('route.default_controller')) . DIRECTORY_SEPARATOR;
        $pages = str_replace('.html', '', get_dir_files($home_view_path));
        View::assign('pages', $pages);

        return parent::detail(); // TODO: Change the autogenerated stub
    }
}
